package com.xuqm.base.web

import android.Manifest
import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.bigkoo.alertview.AlertView
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions
import com.xuqm.base.App
import com.xuqm.base.extensions.getStringForPreferences
import com.xuqm.base.extensions.log
import com.xuqm.base.extensions.runWithPermission
import com.xuqm.base.extensions.showMessage
import com.xuqm.base.viewmodel.BaseViewModel
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_NAME

class XWebViewModel : BaseViewModel() {
    lateinit var callBack: XWebCallBack

    @SuppressLint("StaticFieldLeak")
    lateinit var activity: XWebViewActivity

    private val _title = MutableLiveData<String>()
    val title: LiveData<String> = _title

    fun setTitle(title: String) {
        _title.postValue(title)
    }

    private lateinit var callId: String
    fun scan(callBackId: String) {
        callId = callBackId

        App.getInstance().runOnUiThread {
            activity.runWithPermission(
                Manifest.permission.CAMERA,
                rationaleMethod = {
                    "无法获取相关权限".showMessage()
                    callBackError(callId, "没有相关权限")
                },
                permanentDeniedMethod = {
                    "请在设置界面开放对应权限".showMessage()
                    callBackError(callId, "没有相关权限")
                },
                callback = {
                    ScanUtil.startScan(
                        activity, 1111, HmsScanAnalyzerOptions.Creator().setHmsScanTypes(
                            HmsScan.QRCODE_SCAN_TYPE
                        ).create()
                    );
                })
        }

    }

    fun backScan(msg: String) {
        callBackSuccess(callId, msg)
    }

    fun backScanError() {
        callBackError(callId, "扫码失败")
    }

    private val _progress = MutableLiveData<Int>().also { it.postValue(0) }
    val progress: LiveData<Int> = _progress

    fun setProgress(progress: Int) {
        _progress.postValue(progress)
    }

    fun callBackSuccess(id: String) {
        if (this::callBack.isInitialized) {
            callBack.success(id, null)
        }
    }

    fun callBackSuccess(id: String, data: Any) {
        if (this::callBack.isInitialized) {
            callBack.success(id, data)
        }
    }

    fun callBackError(id: String, data: String) {
        if (this::callBack.isInitialized) {
            callBack.error(id, data)
        }
    }


    fun getUserInfo(callBackId: String) {
        if (this::callBack.isInitialized) {
            callBack.success(callBackId, activity.getStringForPreferences(SHARE_UESR_NAME))
        }
    }

    lateinit var v: AlertView

    fun setTopMenu(id: String, date: List<String>) {
        "================>".log()
        App.getInstance().runOnUiThread {
            if (date.isNotEmpty()) {
                activity.baseBinding.baseToolbar.toolbarMenu.visibility = View.VISIBLE
                activity.baseBinding.baseToolbar.toolbarMenu.setOnClickListener {
                    v = AlertView(
                        "请选择需要操作的内容", "", null, date.toTypedArray(), null, activity,
                        AlertView.Style.ActionSheet
                    ) { _, position ->
                        selected(id, position)
                    }
                    v.show()
                }
            } else {
                activity.baseBinding.baseToolbar.toolbarMenu.visibility = View.GONE
            }
        }
    }

    private fun selected(callBackId: String, data: Int) {
        v.dismissImmediately()
        if (this::callBack.isInitialized) {
            callBack.success(callBackId, data)
        }
    }

}